package com.tydic.mapboxtest;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.PointF;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.JsonObject;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.annotations.Marker;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.annotations.PolygonOptions;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.style.layers.FillLayer;
import com.mapbox.mapboxsdk.style.layers.Filter;
import com.mapbox.mapboxsdk.style.layers.LineLayer;
import com.mapbox.mapboxsdk.style.layers.Property;
import com.mapbox.mapboxsdk.style.layers.PropertyFactory;
import com.mapbox.mapboxsdk.style.layers.SymbolLayer;
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;
import com.mapbox.mapboxsdk.style.sources.Source;
import com.mapbox.services.commons.geojson.Feature;
import com.mapbox.services.commons.geojson.FeatureCollection;
import com.mapbox.services.commons.geojson.LineString;
import com.mapbox.services.commons.geojson.Point;
import com.mapbox.services.commons.models.Position;
import com.tydic.mapboxtest.Model.PlotModel;
import com.tydic.mapboxtest.util.FileUtil;
import com.tydic.mapboxtest.util.MapTool;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineCap;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineColor;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineJoin;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineWidth;

public class MainActivity extends AppCompatActivity {

    private MapView mapView;
    private MapboxMap mapboxMap;

    private Context mContext;

    private List<Position> routeCoordinates = new ArrayList<>();
    private List<PlotModel> polygonList = new ArrayList<>();

    private List<Feature> featureList = new ArrayList<>();
    private List<String> selectedCodes = new ArrayList<>();

    private MarkerOptions markerOptions;
    private Marker marker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Mapbox.getInstance(this, getString(R.string.access_token));
//        Mapbox.getInstance(this, "pk.eyJ1IjoiemhhbmdlYiIsImEiOiJjajF0ZDJnMzEwMHloMndrbW9ramx3c2FyIn0.Q4z-lwYsigteWztqglruOQ");

        setContentView(R.layout.activity_main);
        mContext = this;

        mapView = (MapView) findViewById(R.id.mapView);
        mapView.onCreate(savedInstanceState);
        mapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(final MapboxMap map) {
                mapboxMap = map;
                markerOptions = new MarkerOptions()
                        .position(new LatLng(39.323623222344395, 100.0485810148923))
                        .title("MapBox Marker!")
                        .snippet("Welcome to my markerOptions.");
                marker = new Marker(markerOptions);

               // onLineMap();
//                initRouteCoordinates();

//                clickFab(new LatLng(41.327752, 19.818666));
//                addMarkets();

                // 绘制多边形
                drawPolygon();

                // addMapTextOnePoint();

//                testSymbolLayer();
//                testLayer();

               // moveTo(new LatLng(100.185095685, 39.32981730666667), markerOptions);

//                dealClickListener();
            }
        });

    }


    /**
     * 在线地图 from mapbox studio
     */
    private void onLineMap() {
        // final GeoJsonSource geoJsonSource = new GeoJsonSource("terrain-data");
     // final GeoJsonSource geoJsonSource = new GeoJsonSource("terrain-data", "mapbox://guomenghao.1wo3rvax");
        final GeoJsonSource geoJsonSource = new GeoJsonSource("terrain-data", "mapbox://styles/zhangeb/cj1xfyjx7004i2rn0qk26og29");
//        VectorSource vectorSource = new VectorSource("terrain-data", "mapbox://mapbox.mapbox-terrain-v2");
        mapboxMap.addSource(geoJsonSource);

//        FillLayer terrainData = new FillLayer("fill_layer", "terrain-data");
//        terrainData.withProperties(PropertyFactory.fillColor("#FF4081"));
//        mapboxMap.addLayer(terrainData);

        // final FillLayer fillLayer = (FillLayer) mapboxMap.getLayer("selected_layer");
        // fillLayer.setFilter(Filter.eq("CODE", ""));

        mapboxMap.setOnMapClickListener(new MapboxMap.OnMapClickListener() {
            @Override
            public void onMapClick(@NonNull LatLng point) {
                PointF pointF = mapboxMap.getProjection().toScreenLocation(point);
                List<Feature> features2 = mapboxMap.queryRenderedFeatures(pointF, "zhangye_base");

                ArrayList<Feature> feature2ArrayList = new ArrayList<Feature>();
                feature2ArrayList.addAll(features2);

                for (Feature feature : feature2ArrayList) {
                    if (feature.getProperties().has("CODE")) {
                        mapboxMap.removeAnnotations();

                        String code = feature.getProperties().get("CODE").getAsString();
                        if (selectedCodes.contains(code)) {
                            selectedCodes.remove(code);
                        } else {
                            selectedCodes.add(code);
                        }

                        markerOptions.setPosition(point);
                        markerOptions.setTitle(feature.getProperties().get("CODE").toString());
                        markerOptions.setSnippet(feature.getProperties().get("length").toString());
                        markerOptions.setPosition(point);
                        mapboxMap.addMarker(markerOptions);
                    }
                }

                // fillLayer.setFilter(Filter.in("CODE", selectedCodes.toArray()));
//                geoJsonSource.setGeoJson(FeatureCollection.fromFeatures(featureList));
            }
        });

    }

    private void dealClickListener() {
        mapboxMap.setOnMapClickListener(new MapboxMap.OnMapClickListener() {
            @Override
            public void onMapClick(@NonNull LatLng point) {
                for (int i = 0, size = polygonList.size(); i < size; i++) {
                    PlotModel plotModel = polygonList.get(i);
                    if (MapTool.IsWithIn(point, plotModel.getLatLng())) {
                        if (plotModel.isClick()) {
                            polygonList.get(i).setClick(false);
                            mapboxMap.addPolygon(new PolygonOptions()
                                    .addAll(plotModel.getLatLng())
                                    .fillColor(Color.parseColor("#ff00ff")));
                        } else {
                            polygonList.get(i).setClick(true);
                            mapboxMap.addPolygon(new PolygonOptions()
                                    .addAll(plotModel.getLatLng())
                                    .fillColor(Color.parseColor("#303F9F")));
                        }
//                           setInfoWindow(mapboxMap,plotModel);
                        toastTip(plotModel);
                    }
                }
            }
        });
    }

    private void toastTip(final PlotModel plotModel) {
        Toast.makeText(mContext, plotModel.getName() + "\n" + plotModel.getRemark() + "\n" + plotModel.getCtrDate(), Toast.LENGTH_LONG).show();
    }

    // TODO: 17/4/14   待测试
    private void setInfoWindow(final PlotModel plotModel) {
        mapboxMap.setInfoWindowAdapter(new MapboxMap.InfoWindowAdapter() {
            @Nullable
            @Override
            public View getInfoWindow(@NonNull Marker marker) {
                LinearLayout parent = new LinearLayout(mContext);
                parent.setLayoutParams(new LinearLayout.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                parent.setOrientation(LinearLayout.VERTICAL);
                TextView tvName = new TextView(mContext);
                TextView tvRemark = new TextView(mContext);
                TextView tvOther = new TextView(mContext);
                parent.addView(tvName);
                parent.addView(tvRemark);
                parent.addView(tvOther);
                tvName.setText(plotModel.getName());
                tvRemark.setText(plotModel.getRemark());
                tvOther.setText(plotModel.getCtrDate());

                return parent;
            }
        });


//                           mapboxMap.addMarker(new MarkerViewOptions()
//                                   .position(point)
//                                   .rotation(90)
//                                   .anchor(0.5f, 0.5f)
//                                   .alpha(0.5f)
//                                   .title(plotModel.getName())
//                                   .snippet(plotModel.getRemark()+"\n"+plotModel.getCtrDate())
//                                   .infoWindowAnchor(0.5f, 0.5f)
//                                   .flat(true));
    }

    /**
     * 添加多个点
     */
    private void addMarkets() {
        final Marker marker1 = mapboxMap.addMarker(new MarkerOptions().position(new LatLng(-36.848380, 174.762275)).title("Sky Tower"));
        final Marker marker2 = mapboxMap.addMarker(new MarkerOptions().position(new LatLng(-36.847179, 174.777072)).title("Vector Arena"));
        final Marker marker3 = mapboxMap.addMarker(new MarkerOptions().position(new LatLng(-36.801887, 175.108709)).title("Waiheke Island"));
        final Marker marker4 = mapboxMap.addMarker(new MarkerOptions().position(new LatLng(-36.835059, 174.691237)).title("Waitemata Harbour"));

        // When user clicks the map, animate to new camera location
        mapboxMap.setOnMapClickListener(new MapboxMap.OnMapClickListener() {
            @Override
            public void onMapClick(@NonNull LatLng point) {
                LatLngBounds latLngBounds = new LatLngBounds.Builder()
                        .include(marker1.getPosition())
                        .include(marker2.getPosition())
                        .include(marker3.getPosition())
                        .include(marker4.getPosition())
                        .build();

                mapboxMap.moveCamera(CameraUpdateFactory.newLatLngBounds(latLngBounds, 50));

            }
        });
    }


    /**
     * 点击
     */
    public void clickFab(final LatLng latLng) {
        /*FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                moveTo(latLng, markerOptions);
            }
        });*/
    }

    private void moveTo(final LatLng latLng, MarkerOptions marker1) {
        CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(latLng) // Sets the center of the map to the specified location
                .zoom(13)                            // Sets the zoom level
                .build();
        mapboxMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

        mapboxMap.addMarker(marker1);
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            mapboxMap.addPolygon(new PolygonOptions()
                    .addAll(polygonList.get(0).getLatLng())
                    .fillColor(Color.parseColor("#ff00ff")));

//            addMapText();
        }
    };


    /**
     * 画一个图形边界
     */
    private void drawPolygon() {
        moveTo(new LatLng(39.33180680833333, 100.184298305), markerOptions);
        new Thread(new Runnable() {
            @Override
            public void run() {

                polygonList = dealJsonData();

                List<Feature> featureList = new ArrayList<>();
                List<Feature> markerCoordinates = new ArrayList<>();
                for (PlotModel plotModel : polygonList) {
                    JsonObject jsonObject = new JsonObject();
                    jsonObject.addProperty("MASSIF_ID", plotModel.getId());
                    jsonObject.addProperty("CODE", plotModel.getCODE());
                    jsonObject.addProperty("length", plotModel.getLength());

                    List<Position> routeCoordinates = new ArrayList<Position>();
                    for (LatLng latLng : plotModel.getLatLng()) {
                        routeCoordinates.add(Position.fromCoordinates(latLng.getLongitude(), latLng.getLatitude()));
                    }

                    LineString lineString = LineString.fromCoordinates(routeCoordinates);
                    featureList.add(Feature.fromGeometry(lineString, jsonObject));

                    markerCoordinates.add(Feature.fromGeometry(
                            Point.fromCoordinates(Position.fromCoordinates(
                                    plotModel.getCenterLatlng().getLongitude(),
                                    plotModel.getCenterLatlng().getLatitude())),
                            jsonObject) // Boston
                    );
                }

                FeatureCollection featureCollection = FeatureCollection.fromFeatures(featureList);
                Source geoJsonSource = new GeoJsonSource("source", featureCollection);

                FeatureCollection textFeatureCollection = FeatureCollection.fromFeatures(markerCoordinates);
                Source textGeoJsonSource = new GeoJsonSource("marker-source", textFeatureCollection);

                mapboxMap.addSource(geoJsonSource);
                mapboxMap.addSource(textGeoJsonSource);

                final FillLayer fillLayer = new FillLayer("base_layer", "source").withProperties(
                        PropertyFactory.fillColor("#FF4081"));
                mapboxMap.addLayer(fillLayer);


                final FillLayer selected_fillLayer = new FillLayer("selected_layer", "source").withProperties(
                        PropertyFactory.fillColor("#33ff33"));
                selected_fillLayer.setFilter(Filter.eq("CODE", ""));
                mapboxMap.addLayer(selected_fillLayer);

                SymbolLayer markers = new SymbolLayer("text_layer", "marker-source").withProperties(
                        PropertyFactory.textColor("#3F51B5"),
                        PropertyFactory.textField("{MASSIF_ID}"));
                        // PropertyFactory.textField("{MASSIF_ID}"));
                mapboxMap.addLayer(markers);

                mapboxMap.setOnMapClickListener(new MapboxMap.OnMapClickListener() {
                    @Override
                    public void onMapClick(@NonNull LatLng point) {
                        PointF pointF = mapboxMap.getProjection().toScreenLocation(point);
                        List<Feature> features2 = mapboxMap.queryRenderedFeatures(pointF, "base_layer");

                        ArrayList<Feature> feature2ArrayList = new ArrayList<Feature>();
                        feature2ArrayList.addAll(features2);

                        for (Feature feature : feature2ArrayList) {
                            if (feature.getProperties().has("CODE")) {
                                mapboxMap.removeAnnotations();

                                String code = feature.getProperties().get("CODE").getAsString();
                                if (selectedCodes.contains(code)) {
                                    selectedCodes.remove(code);
                                } else {
                                    selectedCodes.add(code);
                                }

                                markerOptions.setPosition(point);
                                markerOptions.setTitle(feature.getProperties().get("CODE").toString());
                                markerOptions.setSnippet(feature.getProperties().get("length").toString());
                                markerOptions.setPosition(point);
                                mapboxMap.addMarker(markerOptions);
                            }
                        }

                        selected_fillLayer.setFilter(Filter.in("CODE", selectedCodes.toArray()));
                    }
                });

//                //多此一举是防止有些手机一次渲染不完地块，故模拟点击再次渲染
//        handler.sendEmptyMessage(0);

            }
        }).start();
    }

    /**
     * 处理地块json信息
     */
    private List<PlotModel> dealJsonData() {
        List<PlotModel> polygonList_ = new ArrayList<>();
        try {
            String fileName = "test.json";
            // String fileName = "test_one_polygon.json";
            JSONArray jsonArray = new JSONArray(FileUtil.readBigFile(mContext, fileName));
            for (int i = 0, length = jsonArray.length(); i < length; i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String ll = jsonObject.getString("PLACES_JSON");
//                ll.replaceAll("[\\\\[\\\\]]","");
//                ll.replaceAll("\\]","");
                ll = ll.substring(1, ll.length() - 1);
                String[] ll_ = ll.split(",");


                double latMax = 0;
                double latMin = 0;
                double lonMax = 0;
                double lonMin = 0;

                PlotModel plotModel = new PlotModel();
                List<LatLng> polygons = new ArrayList<>();
                int size = ll_.length;
                for (int j = 0; j < size; j = j + 2) {
                    double latitude = Double.valueOf(ll_[j + 1].substring(0, ll_[j + 1].length() - 1));
                    double longitude = Double.valueOf(ll_[j].substring(1));
                    polygons.add(new LatLng(latitude, longitude));

                    if (j == 0) {
                        latMax = latitude;
                        latMin = latitude;
                        lonMax = longitude;
                        lonMin = longitude;
                    }

                    if (latitude > latMax) {
                        latMax = latitude;
                    } else if (latitude < latMin) {
                        latMin = latitude;
                    }

                    if (longitude > lonMax) {
                        lonMax = longitude;
                    } else if (longitude < lonMin) {
                        lonMin = longitude;
                    }
                }
                plotModel.setLatLng(polygons);
                plotModel.setClick(false);
                plotModel.setCODE(jsonObject.has("GIS_CODE") ? jsonObject.getString("GIS_CODE") : "未知");
                plotModel.setLength(jsonObject.has("DRAW_LENGTH") ? jsonObject.getString("DRAW_LENGTH") : "未知");
                plotModel.setId(jsonObject.has("MASSIF_ID") ? jsonObject.getString("MASSIF_ID") : "未知");
                plotModel.setName(jsonObject.has("MASSIF_NAME") ? jsonObject.getString("MASSIF_NAME") : "未知");
                plotModel.setRemark(jsonObject.has("REMARK") ? jsonObject.getString("REMARK") : "未知");
                plotModel.setCtrDate(jsonObject.has("CRT_DATE") ? jsonObject.getString("CRT_DATE") : "未知");
                plotModel.setCenterLatlng(new LatLng((latMax + latMin) / 2, (lonMax + lonMin) / 2));
                plotModel.setMapDisplayText(jsonObject.getString("MASSIF_ID"));

                polygonList_.add(plotModel);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return polygonList_;
    }

    private void addMapTextOnePoint() {
        List<Feature> markerCoordinates = new ArrayList<>();
        markerCoordinates.add(Feature.fromGeometry(
                Point.fromCoordinates(Position.fromCoordinates(100.185095685, 39.32981730666667))) // Boston
                // Common Park
        );
        // markerCoordinates.add(Feature.fromGeometry(
        //         Point.fromCoordinates(Position.fromCoordinates(100.18511498666666, 39.329860605))) // Fenway
        //         // Park
        // );
        // markerCoordinates.add(Feature.fromGeometry(
        //         Point.fromCoordinates(Position.fromCoordinates(100.185288565, 39.32982544666667))) // The
        //         // Paul Revere House
        // );
        FeatureCollection featureCollection = FeatureCollection.fromFeatures(markerCoordinates);

        Source geoJsonSource = new GeoJsonSource("marker-source", featureCollection);
        mapboxMap.addSource(geoJsonSource);

        Bitmap icon = BitmapFactory.decodeResource(this.getResources(), R
                .mipmap.blue_marker_view);

        // Add the marker image to map
        mapboxMap.addImage("my-marker-image", icon);

        SymbolLayer markers = new SymbolLayer("marker-layer", "marker-source")
                .withProperties(PropertyFactory.iconImage("my-marker-image"), PropertyFactory
                        .textField("看看不说话"));
        mapboxMap.addLayer(markers);
    }

    private void addMapText() {
        try {

            SymbolLayer markers = new SymbolLayer("text", "text").withProperties(
                    PropertyFactory.textColor("#3F51B5"),
                    PropertyFactory.textField("{MASSIF_ID}"));
            mapboxMap.addLayer(markers);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 测试数据
     *
     * @return
     */
    private List<LatLng> testData() {
        List<LatLng> polygon = new ArrayList<>();
        polygon.add(new LatLng(45.522585, -122.685699));
        polygon.add(new LatLng(45.534611, -122.708873));
        polygon.add(new LatLng(45.530883, -122.678833));
        polygon.add(new LatLng(45.547115, -122.667503));
        polygon.add(new LatLng(45.530643, -122.660121));
        polygon.add(new LatLng(45.533529, -122.636260));
        polygon.add(new LatLng(45.521743, -122.659091));
        polygon.add(new LatLng(45.510677, -122.648792));
        polygon.add(new LatLng(45.515008, -122.664070));
        polygon.add(new LatLng(45.502496, -122.669048));
        polygon.add(new LatLng(45.515369, -122.678489));
        polygon.add(new LatLng(45.506346, -122.702007));
        polygon.add(new LatLng(45.522585, -122.685699));

        return polygon;
    }

    private void testSymbolLayer() {
//        SymbolLayer cityLabelLayer = mapboxMap.getLayerAs("city_label_big");
//        cityLabelLayer.setProperties(
//                PropertyFactory.textField("ddd")
//        );
//        mapboxMap.addLayer(cityLabelLayer);
    }

    private void testLayer() {
        LineString lineString = LineString.fromCoordinates(routeCoordinates);

        FeatureCollection featureCollection =
                FeatureCollection.fromFeatures(new Feature[]{Feature.fromGeometry(lineString)});

        Source geoJsonSource = new GeoJsonSource("line-source", featureCollection);

        mapboxMap.addSource(geoJsonSource);

        LineLayer lineLayer = new LineLayer("linelayer", "line-source");

        // The layer properties for our line. This is where we make the line dotted, set the
        // color, etc.
        lineLayer.setProperties(
                PropertyFactory.lineDasharray(new Float[]{0.01f, 2f}),
                lineCap(Property.LINE_CAP_ROUND),
                lineJoin(Property.LINE_JOIN_ROUND),
                lineWidth(5f),
                lineColor(Color.parseColor("#e55e5e")),
                PropertyFactory.textField("我就看看不说话")
        );

        mapboxMap.addLayer(lineLayer);

        clickFab(new LatLng(33.38926830725471, -118.38522773650804));
    }


    private void initRouteCoordinates() {
        routeCoordinates.add(Position.fromCoordinates(-118.39439114221236, 33.397676454651766));
        routeCoordinates.add(Position.fromCoordinates(-118.39421054012902, 33.39769799454838));
        routeCoordinates.add(Position.fromCoordinates(-118.39408583869053, 33.39761901490136));
        routeCoordinates.add(Position.fromCoordinates(-118.39388373635917, 33.397328225582285));
        routeCoordinates.add(Position.fromCoordinates(-118.39372033447427, 33.39728514560042));
        routeCoordinates.add(Position.fromCoordinates(-118.3930882271826, 33.39756875508861));
        routeCoordinates.add(Position.fromCoordinates(-118.3928216241072, 33.39759029501192));
        routeCoordinates.add(Position.fromCoordinates(-118.39227981785722, 33.397234885594564));
        routeCoordinates.add(Position.fromCoordinates(-118.392021814881, 33.397005125197666));
        routeCoordinates.add(Position.fromCoordinates(-118.39090810203379, 33.396814854409186));
        routeCoordinates.add(Position.fromCoordinates(-118.39040499623022, 33.39696563506828));
        routeCoordinates.add(Position.fromCoordinates(-118.39005669221234, 33.39703025527067));
        routeCoordinates.add(Position.fromCoordinates(-118.38953208616074, 33.39691896489222));
        routeCoordinates.add(Position.fromCoordinates(-118.38906338075398, 33.39695127501678));
        routeCoordinates.add(Position.fromCoordinates(-118.38891287901787, 33.39686511465794));
        routeCoordinates.add(Position.fromCoordinates(-118.38898167981154, 33.39671074380141));
        routeCoordinates.add(Position.fromCoordinates(-118.38984598978178, 33.396064537239404));
        routeCoordinates.add(Position.fromCoordinates(-118.38983738968255, 33.39582400356976));
        routeCoordinates.add(Position.fromCoordinates(-118.38955358640874, 33.3955978295119));
        routeCoordinates.add(Position.fromCoordinates(-118.389041880506, 33.39578092284221));
        routeCoordinates.add(Position.fromCoordinates(-118.38872797688494, 33.3957916930261));
        routeCoordinates.add(Position.fromCoordinates(-118.38817327048618, 33.39561218978703));
        routeCoordinates.add(Position.fromCoordinates(-118.3872530598711, 33.3956265500598));
        routeCoordinates.add(Position.fromCoordinates(-118.38653065153775, 33.39592811523983));
        routeCoordinates.add(Position.fromCoordinates(-118.38638444985126, 33.39590657490452));
        routeCoordinates.add(Position.fromCoordinates(-118.38638874990086, 33.395737842093304));
        routeCoordinates.add(Position.fromCoordinates(-118.38723155962309, 33.395027006653244));
        routeCoordinates.add(Position.fromCoordinates(-118.38734766096238, 33.394441819579285));
        routeCoordinates.add(Position.fromCoordinates(-118.38785936686516, 33.39403972556368));
        routeCoordinates.add(Position.fromCoordinates(-118.3880743693453, 33.393616088784825));
        routeCoordinates.add(Position.fromCoordinates(-118.38791956755958, 33.39331092541894));
        routeCoordinates.add(Position.fromCoordinates(-118.3874852625497, 33.39333964672257));
        routeCoordinates.add(Position.fromCoordinates(-118.38686605540683, 33.39387816940854));
        routeCoordinates.add(Position.fromCoordinates(-118.38607484627983, 33.39396792286514));
        routeCoordinates.add(Position.fromCoordinates(-118.38519763616081, 33.39346171215717));
        routeCoordinates.add(Position.fromCoordinates(-118.38523203655761, 33.393196040109466));
        routeCoordinates.add(Position.fromCoordinates(-118.3849955338295, 33.393023711860515));
        routeCoordinates.add(Position.fromCoordinates(-118.38355931726203, 33.39339708930139));
        routeCoordinates.add(Position.fromCoordinates(-118.38323251349217, 33.39305243325907));
        routeCoordinates.add(Position.fromCoordinates(-118.3832583137898, 33.39244928189641));
        routeCoordinates.add(Position.fromCoordinates(-118.3848751324406, 33.39108499551671));
        routeCoordinates.add(Position.fromCoordinates(-118.38522773650804, 33.38926830725471));
        routeCoordinates.add(Position.fromCoordinates(-118.38508153482152, 33.38916777794189));
        routeCoordinates.add(Position.fromCoordinates(-118.38390332123025, 33.39012280171983));
    }


    // Add the mapView lifecycle to the activity's lifecycle methods
    @Override
    protected void onStart() {
        super.onStart();
        mapView.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mapView.onStop();
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mapView.onLowMemory();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }
}
